(define-module (wkhtmltopdf)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix utils)
  #:use-module (guix build-system qt)
  #:use-module (guix build-system python)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (gnu packages compression) ; zlib
  #:use-module (gnu packages fontutils)
  #:use-module (gnu packages image)
  #:use-module (gnu packages tls)         ; openssl
  #:use-module (gnu packages xorg)
  #:use-module (gnu packages qt)
  #:use-module (srfi srfi-1))

(define-public wkhtmltopdf
  (package
    (name "wkhtmltopdf")
    (version "0.12.5")
    (source (origin
              (method url-fetch)
              (uri
               (string-append
                "https://github.com/wkhtmltopdf/wkhtmltopdf/archive/"
                version ".tar.gz"))
              (sha256
               (base32
                "08a659qsgn00a3cddh7sx5g0d12vm3ajrpmavbcb5gpmw9hnw7c6"))))
    (build-system qt-build-system)
    (native-inputs `(("qtbase" ,qtbase))) ;; For qmake
    (inputs `(("fontconfig" ,fontconfig)
              ("freetype" ,freetype)
              ("qtwebkit" ,qtwebkit)
              ("qtsvg" ,qtsvg)
              ("qtxmlpatterns" ,qtxmlpatterns)
              ("libpng" ,libpng)
              ("zlib" ,zlib)
              ("libjpeg" ,libjpeg-turbo)
              ("openssl" ,openssl)
              ("libx11" ,libx11)
              ("libxext" ,libxext)
              ("libxrender" ,libxrender)))
    (arguments
     '(#:tests? #f
       #:phases
       (modify-phases %standard-phases
         (replace 'configure
           (lambda* (#:key outputs #:allow-other-keys)
             (let ((out (assoc-ref outputs "out")))
               (substitute* (list "wkhtmltopdf.pro" "src/lib/lib.pro"
                                  ;;"src/shared/shared.pro"
                                  "src/image/image.pro" "src/pdf/pdf.pro")
               ;;(substitute* (find-files ".*pro")
                 (("$(INSTALL_ROOT)") ""))
               (invoke "qmake"
                       (string-append "wkhtmltopdf.pro INSTALLBASE=" out))
               #t)))
         ;; Fails at install
         ;; starting phase `install'
         ;; ...
         ;; make[1]: Entering directory '/tmp/guix-build-wkhtmltopdf-0.12.5.drv-0/wkhtmltopdf-0.12.5/src/lib'
         ;; mkdir: cannot create directory ‘/include’: Permission denied
         ;; make[1]: *** [Makefile:823: install_headers] Error 1
         
    )))

    ;; Meta
    (synopsis "Tools for rendering web pages to PDF or images")
    (description "wkhtmltopdf and wkhtmltoimage are open source (LGPL)
command line tools to render HTML into PDF and various image formats
using the QT Webkit rendering engine.  These run entirely \"headless\" and
do not require a display or display service.

      There is also a C library, if you're into that kind of
thing.

GUIX Specific: C library is untested")
    (home-page "http://wkhtmltopdf.org/")
    (license license:lgpl3+)))

;;;;;;;;;;;;;;;;;;;;;;;;;
;; NIX
;;;;;;;;;;;;;;;;;;;;;;;;;

;;{ mkDerivation, lib, fetchFromGitHub, qtwebkit, qtsvg, qtxmlpatterns
;;, fontconfig, freetype, libpng, zlib, libjpeg
;;, openssl, libX11, libXext, libXrender }:
;;mkDerivation rec {

;;  prePatch = ''
;;    for f in src/image/image.pro src/pdf/pdf.pro ; do
;;      substituteInPlace $f --replace '$(INSTALL_ROOT)' ""
;;    done
;;  '';
;;  configurePhase = "qmake wkhtmltopdf.pro INSTALLBASE=$out";
;;
;;  enableParallelBuilding = true;
;;
;;  meta = with lib; {
;;    homepage = "https://wkhtmltopdf.org/";
;;    description = "Tools for rendering web pages to PDF or images";
;;    longDescription = ''
;;      wkhtmltopdf and wkhtmltoimage are open source (LGPL) command line tools
;;      to render HTML into PDF and various image formats using the QT Webkit
;;      rendering engine. These run entirely "headless" and do not require a
;;      display or display service.
;;
;;      There is also a C library, if you're into that kind of thing.
;;    '';
;;    license = licenses.gpl3Plus;
;;    maintainers = with maintainers; [ jb55 ];
;;    platforms = with platforms; linux;
;;  };
;;}

